package com.ycsj.goldmedalnewconcept.common.activity;


import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropActivity;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.PicassoEngine;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 裁剪图片的测试类
 * Created by Administrator on 2019/5/9 0009.
 */

public class TestCropActivity extends BaseActivity {
    @BindView(R.id.bt)
    Button bt;
    @BindView(R.id.lv)
    ImageView lv;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_test;
    }

    @Override
    protected void init() {

    }

    @OnClick({R.id.bt})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.bt:
                showChoosePic();
                break;
        }
    }


    private static final int REQUEST_CODE_CHOOSE = 23;//定义请求码常量

    private void showChoosePic() {
        Matisse
                .from(TestCropActivity.this)
                .choose(MimeType.ofImage())//照片视频全部显示
                .countable(true)//有序选择图片
                .maxSelectable(1)//最大选择数量为9
                .gridExpectedSize(UiUtils.dp2px(120))//图片显示表格的大小getResources()
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)//图像选择和预览活动所需的方向。
//                .thumbnailScale(0.85f)//缩放比例
                .theme(R.style.Matisse_Zhihu)//主题  暗色主题 R.style.Matisse_Dracula
                .imageEngine(new PicassoEngine())//加载方式
                .forResult(REQUEST_CODE_CHOOSE);//请求码
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_CHOOSE) {
                //选择图片后调用裁剪
                List<Uri> uris = Matisse.obtainResult(data);
                startCrop(uris.get(0));
            } else if (requestCode == UCrop.REQUEST_CROP) {
                //获取裁剪后的Uri
                Uri resultUri = UCrop.getOutput(data);

                lv.setImageURI(resultUri);
            }

        }
    }


    /**
     * 开启图片裁剪库
     *
     * @param uri
     */
    private void startCrop(@NonNull Uri uri) {

        UCrop uCrop = UCrop.of(uri, Uri.fromFile(new File(getCacheDir(), "SampleCropImage.jpg")));
        //初始化UCrop配置
        UCrop.Options options = new UCrop.Options();
        //设置裁剪图片可操作的手势
        options.setAllowedGestures(UCropActivity.SCALE, UCropActivity.ROTATE, UCropActivity.ALL);
        //是否隐藏底部容器，默认显示
        options.setHideBottomControls(false);
        //设置toolbar颜色
        options.setToolbarColor(ActivityCompat.getColor(TestCropActivity.this, R.color.colorPrimary));
        //设置状态栏颜色
        options.setStatusBarColor(ActivityCompat.getColor(TestCropActivity.this, R.color.colorPrimary));
        //是否能调整裁剪框
        options.setFreeStyleCropEnabled(true);
        //UCrop配置
        uCrop.withOptions(options);

        uCrop.start(TestCropActivity.this);
    }
}
